package com.example.administrator.managesystem.view;


import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.managesystem.R;


/**
 * 状态布局，封装了4种状态：正在加载、加载失败、加载为空、正常界面
 * 使用{@link #newInstance(Context, View)} 来创建StateLayout的实现
 *
 * @author wr
 */
public class StateLayout extends FrameLayout {

    private FrameLayout loadingView;
    private FrameLayout failView;
    private FrameLayout emptyView;
    private View contentView;
    //默认empty
    private View mDefaultEmpty;
    //默认loading
    private View mDefaultLoading;
    //默认 fail
    private View mDefaultFail;
    private AnimationDrawable mAnimationDrawable;
    public boolean mNeedHideAnimal = true;


    public StateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 创建一个StateLayout实现
     *
     * @param contentView      正常想要展示的View
     */
    public static StateLayout newInstance(Context context, View contentView) {
        StateLayout stateLayout = (StateLayout) LayoutInflater.from(context).inflate(R.layout.state_layout, null);
        stateLayout.contentView = contentView;
        // StateLayout inflate之后就有3个状态的View了，还需要第四种状态
        stateLayout.addView(contentView);
        //当添加完成后暂时给隐藏掉
        contentView.setVisibility(View.INVISIBLE);
        return stateLayout;
    }

    /**
     * 渲染完毕后展示获取view对象
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        loadingView = findViewById(R.id.loadView);
        failView = findViewById(R.id.failView);
        emptyView = findViewById(R.id.emptyView);
        showLoadingView();
    }

    /**
     * 显示正在加载的View
     */
    public void showLoadingView() {
        if (mDefaultLoading == null) {
            mDefaultLoading = LayoutInflater.from(getContext()).inflate(R.layout.commom_layout_loading, null);
            loadingView.removeAllViews();
            loadingView.addView(mDefaultLoading);
            ImageView defaultImage = mDefaultLoading.findViewById(R.id.img_progress);
            mAnimationDrawable = (AnimationDrawable) defaultImage.getDrawable();
            // 开始动画
            if (!mAnimationDrawable.isRunning()) {
                mAnimationDrawable.start();
            }
        }
        showView(loadingView);
    }

    public void setLoadingView(View view) {
        mDefaultLoading = view;
        loadingView.removeAllViews();
        loadingView.addView(mDefaultLoading);
    }

    /**
     * 显示失败的View
     */
    public void showFailView() {
        if (mDefaultFail == null) {
            mDefaultFail = LayoutInflater.from(getContext()).inflate(R.layout.commom_layout_loading_error, null);
            failView.removeAllViews();
            failView.addView(mDefaultFail);
            failView.findViewById(R.id.root).setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (listener != null) {
                        listener.clickFaliure();
                    }
                }
            });
        }
        showView(failView);
    }

    public void setFailView(View view) {
        mDefaultFail = view;
        failView.removeAllViews();
        failView.addView(mDefaultFail);
        failView.findViewById(R.id.root).setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (listener != null) {
                    listener.clickFaliure();
                }
            }
        });
    }

    /**
     * 显示加载为空的View
     */
    public void showEmptyView() {
        if (mDefaultEmpty == null) {
            mDefaultEmpty = LayoutInflater.from(getContext()).inflate(R.layout.commom_layout_loading_no_data, null);
            emptyView.removeAllViews();
            emptyView.addView(mDefaultEmpty);
            emptyView.findViewById(R.id.root).setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (listener != null) {
                        listener.clickFaliure();
                    }
                }
            });
        }
        showView(emptyView);
    }

    public void setEmptyContent(String content){
        TextView defaultContent = mDefaultEmpty.findViewById(R.id.defaultContent);
        defaultContent.setText(content);
    }

    public void setEmptyView(View view) {
        mDefaultEmpty = view;
        emptyView.removeAllViews();
        emptyView.addView(mDefaultEmpty);
        view.findViewById(R.id.root).setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (listener != null) {
                    listener.clickFaliure();
                }
            }
        });
    }

    /**
     * 显示正常界面的View
     */
    public void showContentView() {
        if (mNeedHideAnimal){
            Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.screen_zoom_out);
            loadingView.startAnimation(animation);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
        if (mAnimationDrawable.isRunning()) {
            mAnimationDrawable.stop();
        }
        showView(contentView);
    }

    /**
     * 显示指定的View，并且隐藏其它的View
     *
     * @param view 指定要显示的View
     */
    private void showView(View view) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);    //
            child.setVisibility(view == child ? View.VISIBLE : View.INVISIBLE);
        }
    }

    private onLaoutClickListener listener;

    public interface onLaoutClickListener {
        void clickFaliure();
    }

    public void setoOnLaoutClickListener(onLaoutClickListener listener) {
        this.listener = listener;

    }

}
